package com.bjtu.douyin.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;

import java.io.IOException;

public class ProtobufSerializer extends StdSerializer<Message> {

    public ProtobufSerializer() {
        this(null);
    }

    public ProtobufSerializer(Class<Message> t) {
        super(t);
    }

    @Override
    public void serialize(
            Message value, JsonGenerator gen, SerializerProvider provider)
            throws IOException {
        JsonFormat.Printer printer = JsonFormat.printer().omittingInsignificantWhitespace();
        String json = printer.print(value);
        gen.writeRawValue(json);
    }
}
